package com.speedchina.permission.security.shiro;

import com.speedchina.common.redis.service.RedisService;
import com.speedchina.framework.utils.ApplicationContextHolder;
import com.speedchina.permission.domain.sys.entity.User;
import com.speedchina.permission.security.jwt.JwtUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * SecurityUtils
 *
 * @author Winter
 * @create 2020-11-12 下午4:03
 **/

public class SecurityUtils {

    /**
     * 获取当前登录用户
     * @return
     */
    public static User getLoginUser() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();

        JwtUtil jwtTools = ApplicationContextHolder.getBean(JwtUtil.class);
        return jwtTools.getLoginUser(request);
    }

    /**
     * 登出
     */
    public static boolean logout() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        JwtUtil jwtTools = ApplicationContextHolder.getBean(JwtUtil.class);
        RedisService redisService = ApplicationContextHolder.getBean(RedisService.class);
        return redisService.del(jwtTools.getJwtToken(request));
    }
    /**
     * 当前用户token
     */
    public static String getToken() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        JwtUtil jwtTools = ApplicationContextHolder.getBean(JwtUtil.class);
        return jwtTools.getJwtToken(request);
    }

}
